// routes/order.router.js
const express = require('express');
const { body, query, param } = require('express-validator');
const {authMiddleware} = require('../tool/middleWare');
const express_validate = require('../tool/express_validator');
const {asyncHandler} = require('../tool/middleWare');
const {
    createOrderService,
    getUserOrdersService,
    getOrderDetailService,
    advisorReplyOrderService,
} = require('../service/order');

const router = express.Router();

/** 3.1 用户创建订单 */
router.post(
    '/create',
    authMiddleware,
    body('advisorId').isInt({ min: 1 }).toInt(),
    body('serviceId').isInt({ min: 1 }).toInt(),
    body('generalSituation').optional().isString().trim().isLength({ max: 5000 }),
    body('specificQuestion').optional().isString().trim().isLength({ max: 200 }),
    body('isRush').optional().isInt({ min: 0, max: 1 }).toInt(),
    body('attachmentList').optional().isArray({ max: 20 }),
    body('attachmentList.*.fileName').isString().trim().isLength({ min: 1 }),
    body('attachmentList.*.fileType').isString().trim().isLength({ min: 1 }),
    body('attachmentList.*.fileUrl').isString().trim().isLength({ min: 1 }),
    express_validate,
    asyncHandler(async (req, res) => {
        const userId = req.loginUser.id;
        const data = await createOrderService({ userId, ...req.body });
        res.success(data, '订单创建成功');
    })
);

/** 3.2 用户查看订单列表 */
router.get(
    '/list',
    authMiddleware,
    query('pageSize').optional().isInt({ min: 1, max: 100 }).toInt(),
    query('offset').optional().isInt({ min: 0 }).toInt(),
    express_validate,
    asyncHandler(async (req, res) => {
        const userId = req.loginUser.id;
        const { pageSize = 10, offset = 0 } = req.query;
        const data = await getUserOrdersService({ userId, pageSize, offset });
        res.success(data, 'ok');
    })
);

/** 3.3 用户查看订单详情 */
router.get(
    '/detail',
    authMiddleware,
    query('orderId').isInt({ min: 1 }).toInt(),
    express_validate,
    asyncHandler(async (req, res) => {
        const userId = req.loginUser.id;
        const orderId = Number(req.query.orderId);
        const data = await getOrderDetailService({ userId, orderId });
        res.success(data, 'ok');
    })
);

/** 3.4 顾问回复订单 */
router.post(
    '/reply',
    authMiddleware, // 顾问登录
    body('orderId').isInt({ min: 1 }).toInt(),
    body('replyType').isIn(['text', 'audio']),
    body('replyMessage').if(body('replyType').equals('text')).isString().trim().isLength({ min: 1 }),
    body('replyAudioUrl').if(body('replyType').equals('audio')).isString().trim().isLength({ min: 1 }),
    express_validate,
    asyncHandler(async (req, res) => {
        const advisorId = req.loginUser.id;
        const { orderId, replyType, replyMessage, replyAudioUrl, audioDurationSec } = req.body;
        await advisorReplyOrderService({
            advisorId,
            orderId: Number(orderId),
            replyType,
            replyMessage,
            replyAudioUrl,
            audioDurationSec,
        });
        res.success(null, '回复成功');
    })
);

module.exports = router;